<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Progress extends Base
{
    //进度页
    public function index()
    {
        $user_id = \think\Session::get('user_id');
        //查预约清单
        $res1 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->join('hospital h','a.hospital_id=h.hospital_id')
            ->where('a.user_id',$user_id)
            // ->field('child_name,vaccine_name,begin_time')
            ->where('appointment_status',0)
            ->order('appointment_id desc')
            ->select();
        //  dump($res1);
        // exit;
        $this->assign("res1",$res1);
         //查预约通过清单
        $res2 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->join('hospital h','a.hospital_id=h.hospital_id')
            ->where('a.user_id',$user_id)
            ->where('appointment_status',1)
            ->order('appointment_id desc')
            ->select();
        $this->assign("res2",$res2);
       //查预约取消清单
       $res3 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->join('hospital h','a.hospital_id=h.hospital_id')
            ->where('a.user_id',$user_id)
            ->where('appointment_status',2)
            ->order('appointment_id desc')
            ->select();
        $this->assign("res3",$res3);
        //查预约全部清单
        $res4 = DB::name('appointment')
            ->alias('a')
            ->join('child c','a.child_id=c.child_id')
            ->join('vaccine v','a.vaccine_id=v.vaccine_id')
            ->join('hospital h','a.hospital_id=h.hospital_id')
            ->where('a.user_id',$user_id)
            ->order('appointment_id desc')
            ->select();
        $this->assign("res4",$res4);
        return $this->fetch();
    }
    //处理取消
    public function updatestatus($appointment_id)
    {
        // dump($appointment_id);
        // exit;
        $data = [
            'appointment_status'=>2,
        ];
        $res = Db::name('appointment')->where('appointment_id',$appointment_id)->update($data);
        if($res)
        {
            return $this->success('取消成功','progress/index');
        }
        else{
            return  $this->error('失败，请重新操作');
        }
    }
    //重新预约
    public function newstatus($appointment_id)
    {
        
        $data = [
            'appointment_status'=>0,
        ];
        $res = Db::name('appointment')->where('appointment_id',$appointment_id)->update($data);
        if($res)
        {
            return $this->success('预约成功','progress/index');
        }
        else{
            return  $this->error('失败，请重新操作');
        }
    }
    // 修改预约
    public function xiugai($id)
     {
        $user_id = \think\Session::get('user_id');
        $aa = DB::name('appointment')
        ->alias('a')
        ->join('child c','a.child_id=c.child_id')
        ->join('vaccine v','a.vaccine_id=v.vaccine_id')
        ->join('hospital h','a.hospital_id=h.hospital_id')
        ->where('a.user_id',$user_id)
        ->where('appointment_id',$id)
        ->find();
        // dump($aa);
        // exit;
    $this->assign("aa",$aa);
        $this->assign("id",$id);
        $user_id = \think\Session::get('user_id');
        $res1 = Db::name('child')
                ->where('user_id',$user_id)
                ->field(['child_id','child_name'])
                ->select();
        $this->assign("res1",$res1);
        $res2 = Db::name('vaccine')
                ->field('vaccine_id,vaccine_name')
                ->select();
        $this->assign("res2",$res2);
        $res3 = Db::name('hospital')
                ->field('hospital_id,hospital_name')
                ->select();
        $this->assign("res3",$res3);
        // dump($res1);
        // dump($res2);
        // dump($res3);
        // exit;
         return $this->fetch();
     }
}